package com.superpai.activity;

import java.util.List;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.superpai.R;
import com.superpai.domain.DBProduct;
import com.superpai.utils.AndroidUtils;
import com.superpai.view.adapter.CartAdapter;
import com.superpai.view.db.CartDBHelper;

/**
 * @author Devin.Hu
 * @date 2011-8-13
 * @version V1.0
 * @description
 */
public class CartActivity extends BaseActivity {
	
	private List<DBProduct> mList;
	private CartAdapter mAdapter;
	private ListView cart_listview;
	
	public TextView mNodata;
	
	public TextView cart_productnum;
	public TextView cart_marketprice;
	public TextView cart_offer;
	public TextView cart_itemprice;
	private Button cart_submit;
	private final int GETCARTLIST = 1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart);
        initMenuView(this, mIndexCart);
        initViews();
    }

    private void initViews(){
		title = (TextView)this.findViewById(R.id.title);
		title.setText(R.string.menu_cart);
		
		cart_productnum = (TextView)this.findViewById(R.id.cart_productnum);
		cart_marketprice = (TextView)this.findViewById(R.id.cart_marketprice);
		cart_offer = (TextView)this.findViewById(R.id.cart_offer);
		cart_itemprice = (TextView)this.findViewById(R.id.cart_itemprice);
		cart_submit = (Button)this.findViewById(R.id.cart_submit);
		cart_submit.setOnClickListener(this);
		
		mProgress = (ProgressBar)this.findViewById(R.id.progress);
		mRefresh = (Button)this.findViewById(R.id.refresh);
		mRefresh.setOnClickListener(this);
		mNodata = (TextView)this.findViewById(R.id.nodata);
		
		mAdapter = new CartAdapter(mContext, this, productCartnum);
		cart_listview = (ListView)this.findViewById(R.id.cart_listview);
		cart_listview.setAdapter(mAdapter);
		
		connectionDate();
	}
    
    /**
	 * 读取店铺信息
	 */
	private void connectionDate(){
		mRefresh.setVisibility(View.GONE);
		mNodata.setVisibility(View.GONE);
		mProgress.setVisibility(View.VISIBLE);
		
		onConnection(GETCARTLIST);
	}
    
	@Override
	public Object doDataConnection(int type) {
		CartDBHelper db = new CartDBHelper(mContext);
		mList = db.findItemList();
		if(null != mList && mList.size() > 0){
			return STATUS_SUC;
		}
		return STATUS_NODATA;
	}

	@Override
	public void doProcessData(int type, Object... values) {
		int status = Integer.parseInt(values[0].toString());
		mProgress.setVisibility(View.GONE);
		
		if(status == STATUS_SUC){
			fillCartDate();
			mAdapter.setmList(mList);
			mAdapter.notifyDataSetChanged();
		}else{
			mNodata.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 填充购物车信息
	 */
	private void fillCartDate() {
		CartDBHelper db = new CartDBHelper(mContext);
		cart_productnum.setText(db.getCartItemNum(mList)+"");
		cart_marketprice.setText(db.getCartMPrice(mList)+"");
		cart_offer.setText(db.getOfferPrice(mList)+"");
		cart_itemprice.setText(db.getCartTotalPrice(mList)+"");
	}

	
	@Override
	public void onClick(View v) {
		checkMenu(v);
		
		if(v == cart_submit){
			AndroidUtils.Toast(mContext, mContext.getString(R.string.common_nosupport));
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			new AlertDialog.Builder(this)
			.setTitle(R.string.common_tishi)
			.setMessage(R.string.common_sys_exit)
			.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					sysExit();
				}
			})
			.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.show();
			return true;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		disConnection();
	}
	
}